package com.jim.controller;

import org.apache.log4j.Logger;
import com.jim.base.JsonResult;
import com.jim.entity.User;
import com.jim.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class UserController {


    private Logger logger = Logger.getLogger(UserController.class);


    @Autowired
    private UserService userService;

    //http://localhost:8098/home
    @RequestMapping(value="/home")
    public String goHomeByModeAndview(){
         System.out.println("go to the home page!");
         String test ="go to the home page";
         return test;
    }

    /**	 * 根据ID查询用户	 *
     * @param id
     * * @return
     * */
    @RequestMapping(value = "user/{id}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){
        JsonResult r = new JsonResult();
        try {
            User user = userService.getUserById(id);
            r.setResult(user);
            r.setStatus("ok");
            System.out.println("go to the home page!");
            logger.info("===================================================");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("error");
            e.printStackTrace();
        }
            return ResponseEntity.ok(r);
    }


}
